<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="xms">
    <meta name="author" content="Xms">
    <link rel="icon" href="/content/favicon.ico">

    <title>系统 - 监控 - 数据日志</title>

    <!-- Bootstrap core CSS -->
    <link href="/content/css/bootstrap3.3.5/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/content/css/font-awesome.min.css" rel="stylesheet">
    <link href="/content/css/common.css" rel="stylesheet">
    <link href="/content/js/jquery-toast/jquery.toast.min.css" rel="stylesheet">
    <link id="themeLink" href="/content/css/theme/default.css" rel="stylesheet"/>
    <!-- Bootstrap core JavaScript ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="/content/js/jquery.min.js"></script>
    <script src="/content/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="/content/js/ie10-viewport-bug-workaround.js"></script>
    <script src="/content/js/jquery.bootstrap.min.js"></script>
    <script src="/content/js/json2.js"></script>
    <script src="/content/js/xms.utility.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.core.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.widget.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js"></script>
    <script src="/content/js/jquery-toast/jquery.toast.min.js"></script>
    <script src="/content/js/xms.jquery.js"></script>
    <script src="/content/js/xms.web.js"></script>
    <script src="/content/js/commonlabels.js"></script>
    <script src="/content/js/common/pagecontext.js"></script>
    <script src="/api/user/usercontext"></script>
    <script>
        jQuery(function () {
            Xms.Web.Loading();
            if (typeof parent !== 'undefined') {
                if (parent != window) {//判断当前页面是否为子页面
                    $('body').addClass('xms-subpage');
                }
            }
        });

        function changeTheme(body, themename) {
            $("#themeLink").attr('href', '/content/css/theme/' + themename + '.css');
        }
    </script>
    <link href="/content/js/bootstrap-datepicker-1.5.0/css/bootstrap-datepicker3.min.css" rel="stylesheet">
    <link href="/content/js/jquery-ui-1.10.3/themes/base/jquery.ui.all.css" rel="stylesheet">
    <link href="/content/js/grid/pqgrid.dev.css" rel="stylesheet">
    <style>
        body {
            padding-top: 0;
        }
    </style>
</head>

<body data-spy="scroll" data-target="#myScrollspy">
<div class="container-fluid" id="body">
    <div class="row">
        <div class="shadeBox"
             style="background-color:#000000; opacity:0.5;width:100%;position:absolute;z-index:10;display:none;"></div>
        <div class="col-md-12" id="main" style="margin-bottom:80px;">
            <div><br class="breadcrumb"/></div>
            <div id="content"></div>
            <div class="page-render-wrap">
                <!--main-->

                <div class="" id="gridview">
                    <div class="mb-2 toolbar">
                        <div class="">
                            <!--in-->
                            <div class="panel-body">
                                <form action="/xms/api/log/entitylog/list" class="form-horizontal" id="searchForm"
                                      method="get" role="form">
                                    <input id="EntityId" name="entityid" type="hidden"/>
                                    <input type="hidden" name="loaddata" value="true"/>
                                    <div class="row">
                                        <div class="col-sm-10 pl-0 pull-right">
                                            <div class="input-group input-group-sm">
                                <span class="input-group-btn input-group-sm" style="width:300px;">
                                    <input type="text" class="form-control input-sm hide" id="viewSelector"
                                           style="width:150px;"/>
                                </span>
                                                <select class="form-control input-sm" name="operationtype">
                                                    <option value="">事件</option>
                                                    <option value="1">创建</option>
                                                    <option value="2">修改</option>
                                                    <option value="3">删除</option>
                                                    <option value="4">共享</option>
                                                    <option value="5">分派</option>
                                                </select>

                                                <span class="input-group-btn ">
                                    <button type="submit" class="btn btn-primary btn-sm btn-block"
                                            style="border-radius:0;"><span class="glyphicon glyphicon-search"></span> 查询</button>
                                </span>
                                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-warning btn-sm btn-block"
                                            data-role="clearForm"><span
                                            class="glyphicon glyphicon-remove"></span> 清空</button>
                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <div class="datagrid-view"></div>
                        <table class="table table-striped table-hover table-condensed" id="datatable"
                               data-pageurl="/xms/log/entitylog" data-refresh="rebind()" data-ajax="true"
                               data-ajaxcontainer="gridview" data-ajaxcallback="ajaxgrid_reset()"
                               data-sortby="createdon" data-sortdirection="1" data-dbclick="rebind()">

                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>


            </div>
            <nav class="navbar navbar-default navbar-fixed-bottom hide" role="navigation" id="body-footer">
                <div class="container">
                    <div class="navbar-form navbar-right" id="body-footer-content">
                    </div>
                </div>
            </nav>
        </div>
    </div>
</div>

<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.button.js"></script>
<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js"></script>
<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.autocomplete.js"></script>
<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js"></script>
<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.resizable.js"></script>
<script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.tooltip.js"></script>
<script src="/content/js/fetch.js"></script>
<script src="/content/js/common/filters.js"></script>
<script src="/content/js/grid/pqgrid.dev.js"></script>
<script src="/content/js/grid/localize/pq-localize-zh.js"></script>
<script src="/content/js/cdatagrid.js"></script>
<script src="/content/js/jquery.bootpag.min.js"></script>
<script src="/content/js/jquery.form.js"></script>
<script src="/content/js/bootstrap-datepicker-1.5.0/js/bootstrap-datepicker.min.js"></script>
<script src="/content/js/bootstrap-datepicker-1.5.0/locales/bootstrap-datepicker.zh-CN.min.js" charset="UTF-8"></script>
<script src="/content/js/xms.metadata.js"></script>
<script>
    var pageUrl = '';
    var current_attributes = [];
    $(function () {
        $('#main').css('margin-bottom', 0);

        $('body').on('change', '#viewSelector', function () {
            $('#EntityId').val(Xms.Web.SelectedValue($(this)));
            //rebind();
            //$('.collapse').removeClass('in');
        });
        $('.datepicker').datepicker({
            autoclose: true
            , clearBtn: true
            , format: "yyyy-mm-dd"
            , language: "zh-CN"
        });

        $('body').on('click', '[data-role="clearForm"]', function (e) {
            clearEntityLogs()
        })
        loadEntitys($('#viewSelector'));
        //$("#datatable").ajaxTable();
        //ajaxgrid_reset();
    });

    var initState = false;
    function initList(){
        if(initState) return;
        var theaders = {
            'name': ' 类型',
            'default': ' 更改前',
            'updataby': '>更改者',
            'statecode': '更改后',
            'createdon': '时间'
        }
        //列数据配置数据
        var columnConfigs = [
            //从新配置复选框列的渲染方式，
            {
                title: "", dataIndx: "recordid", maxWidth: 48, minWidth: 48, align: "center", resizable: false,
                type: 'checkBoxSelection', cls: 'ui-state-default', sortable: false, editable: false,
                render: function (ui) {
                    //  console.log(ui)
                    return '<input type="checkbox" value="' + ui.rowData.entitylogid + '" name="recordid" class="">'
                },
                cb: {all: true, header: true}
            },
            {
                title: "", dataIndx: "name", maxWidth: 30, minWidth: 30, align: "center", resizable: false,
                cls: 'ui-state-default', sortable: false, editable: false,
                hidden: true,
                render: function (ui) {
                    //  console.log(ui)
                    return '<input type="hidden" value="' + ui.rowData.entitylogid + '" name="componenttypename" class="">'
                },
                cb: {all: true, header: true}
            },
            {
                "dataIndx": "operationtype",
                "title": theaders.name,
                editable: false,
                "dataType": "string",
                "width": 75,
                "isprimaryfield": false,
                "attributetypename": "string",
                render: function (ui, a, b) {
                    var datas = ui.rowData;
                    var dataIndx = ui.dataIndx;
                    var column = ui.column;
                    var recordid = datas[dataIndx];
                    var html = '';
                    if (recordid == 1) {
                        html = '创建';
                    } else if (recordid == 2) {
                        html = '修改';
                    } else if (recordid == 3) {
                        html = '删除';
                    } else if (recordid == 5) {
                        html = '分派';
                    } else if (recordid == 4) {
                        html = '共享';
                    }
                    return html;
                }
            },
            {
                "dataIndx": "isdefault",
                "title": theaders.default,
                "dataType": "string",
                editable: false,
                "width": 300,
                sortable: false,
                "isprimaryfield": false,
                "attributetypename": "string",
                render: function (ui, a, b) {
                    var datas = ui.rowData;
                    var dataIndx = ui.dataIndx;
                    var column = ui.column;
                    var recordid = datas['changedata'];
                    var type = datas['operationtype'];
                    var htmls = ['<table class="table table-condensed">']
                    if (recordid && recordid != '') {
                        var records = JSON.parse(recordid);
                        if (type == 2 || type == 5) {
                            $.each(records, function (i, n) {
                                var attrname = n.name;
                                var attrdatas = $.queryBykeyValue(current_attributes, 'name', attrname);
                                if (attrdatas.length > 0) {
                                    htmls.push(' <tr><td>' + (attrdatas[0].localizedname) + '</td><td>' + n.original + '</td></tr>');
                                } else {
                                    htmls.push(' <tr><td>' + (attrname) + '</td><td>' + n.original + '</td></tr>');
                                }
                            });
                        }
                    }
                    htmls.push('</table>');
                    return htmls.join('')
                }
            },

            {
                "dataIndx": "statecode",
                "title": theaders.statecode,
                editable: false,
                sortable: false,
                "dataType": "string",
                "width": 300,
                "isprimaryfield": false,
                "attributetypename": "string",
                render: function (ui, a, b) {
                    var datas = ui.rowData;
                    var dataIndx = ui.dataIndx;
                    var column = ui.column;
                    var recordid = datas['changedata'];
                    var type = datas['operationtype'];
                    var htmls = ['<table class="table table-condensed">']
                    if (recordid && recordid != '') {
                        var records = JSON.parse(recordid);
                        $.each(records, function (i, n) {
                            var attrname = n.name;
                            var attrdatas = $.queryBykeyValue(current_attributes, 'name', attrname);
                            if (attrdatas.length > 0) {
                                htmls.push(' <tr><td>' + (attrdatas[0].localizedname) + '</td><td>' + n.value + '</td></tr>');
                            } else {
                                htmls.push(' <tr><td>' + (attrname) + '</td><td>' + n.value + '</td></tr>');
                            }
                        });

                    }
                    htmls.push('</table>');
                    return htmls.join('')
                }
            },
            {
                "dataIndx": "createdon",
                "title": theaders.createdon,
                editable: false,
                "dataType": "string",
                "width": 100,
                "isprimaryfield": false,
                "attributetypename": "string"
            }
        ];
        var url = ORG_SERVERURL + '/api/log/entitylog/list';
        $('#clearForm').attr('action', ORG_SERVERURL + '/api/log/entitylog/clear');
        var $form = $('#searchForm');
        var roles_filters = new XmsFilter();
        var datagridconfig = {
            scrollModel: {autoFit: true},
            baseUrl: url,
            method: 'GET',
            columnConfigs: columnConfigs,//字段配置信息
            context: $('#gridview'),//底部操作按钮方法触发
            filters: roles_filters,//post提交时过滤条件
            searchForm: $form//GET提交时查询的数据
            , offsetHeight: -50,
            datasFilter: function (data) {
                console.log(data);
                if(data)
                    current_attributes = data.attributes;
            }
        };
        $('.datagrid-view').xmsDataTable(datagridconfig);
        initState = true;
    }

    function loadEntitys($context, callback) {
        var entitySelect = $context;
        entitySelect.entitySelector({
            rendered: function (self) {
                var _entityid = $('#EntityId').val();
                if (_entityid) {
                    var acli = self.listWrap.find('.xms-autoc-item[value="' + _entityid + '"]');
                    if (acli.length > 0) {
                        self.box.val(_entityid);
                        self.listWrap.hide();
                        self.vInput.val(acli.text());
                        self.value = acli.attr('value');
                        self.wrap.attr('data-isactive', false);
                        self.filterData();
                    }
                }
                callback && callback();
            },
            submithandler: function (self, $this) {
                $('#EntityId').val(self.value);
                initList();
            },
            removehandler: function () {
                $('#EntityId').val(self.value);

            },
            inputPlaceHolder: '选择实体'
        });

    }

    function clearEntityLogs() {
        var _entityid = $('#EntityId').val();
        if(!_entityid){
            Xms.Web.Toast('先选择一个实体', false);
            return;
        }
        var _url = ORG_SERVERURL + '/api/log/entitylog/clearbyentity/' + _entityid;
        Xms.Web.Confirm('确认', '是否清空当前实体的日志', function () {
            Xms.Ajax.Get(_url, {}, function (res) {
                Xms.Web.Toast(res.Content || LOC_CLEAR_SUCCESS, res.IsSuccess);
                if(res.IsSuccess) rebind();
            }, null);
        })
    }

    function rebind() {
        $('.datagrid-view').cDatagrid('refreshDataAndView')
        // $('#searchForm').submit();
    }

    function loadEntities(callback) {
        Xms.Schema.GetEntities({'getall': true}, function (data) {
            if (!data || !data.Content) return;
            $(data.Content).each(function (i, n) {
                $('#viewSelector').append('<option data-relationship="' + n.name + '"  value="' + n.entityid + '">' + n.localizedname + '</option>');
            });
            callback && callback();
        });
    }

    function getKeyValue() {
        $('.dataList').each(function (i, n) {
            $(n).each(function (ii, nn) {
                var KeyTarget = $(nn).find('.ChangeKey');
                var ValTarget = $(nn).find('.ChangeValue');
                var data = JSON.parse(decodeURI(KeyTarget.attr('data-json')));
                for (var j = 0; j < data.length; j++) {
                    var key = data[j].key;
                    var val = data[j].value;
                    KeyTarget.append('<span>' + key + '</span><br />');
                    ValTarget.append('<span>' + val + '</span><br />');

                }
                var userid = $(n).find('.UserName').attr('data-id');
                Xms.Web.GetJson(ORG_SERVERURL + '/api/data/retrieve/referencedrecord/' + lookupid + '/' + value, null, function (response) {

                });
            });
        });
    }

    function getUser() {
    }
</script>

</body>
</html>